package com.hnkywl.sts.action.cwgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.VoucherType;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.cwgl.FphbService;
import com.hnkywl.sts.service.cwgl.VoucherService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:57
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/fphb")
public class FphbAction extends SimpleActionSupport {
    private FphbService fphbService;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //申报人所在公司的Id
    private Long gsId;
    //公司的名称
    private String gsJc;
    //申请（查询）开始时间
    private Date hbrqStart;
    //申请（查询）结束时间
    private Date hbrqEnd;

    private Long jsdId;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private VoucherService voucherService;
    private String systemDate;
    private Yg yg;

    private YgService ygService;

    private Map<String, Object> voucher;

    private Integer attachment;

    private Map<String, String> result = new HashMap<String, String>();

    private XmlParse xmlParse;

    private Long hbjsdId;

    private String hbrName;

    private String gsmc;

    private Map<String, Object> voucherMaster = new HashMap<String, Object>();
    private List<Map> voucherItem;

    //发票合并的查询分页显示
    @Action("fphbList")
    public void fphbList() {
        this.renderJson(this.fphbService.findAllfp(hbrName,null,gsId, gsJc,hbrqStart,hbrqEnd,page, rows));
    }

    @Action("fphb")
    public String fp(){
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("fphb_cancel")
    public void fphbCancel() {
        try {
            fphbService.fphbCancel(hbjsdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"解除合并成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("fphb_content")
    public String fphb_content(){
        gsmc = this.fphbService.findFphbDetail(hbjsdId).getGs().getJc().trim();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        voucherMaster = fphbService.findFphbVoucherMain(hbjsdId);
        voucherMaster.put("createdate", simpleDateFormat.format(new Date()));
        voucherItem = fphbService.getFphbVoucherItem(hbjsdId);
        return SUCCESS;
    }

    @Action("generate_voucher")
    public void insert_fpdr(){
        try {
            voucherService.checkJiuqiUsernamePassword();
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            Boolean begin = voucherService.beginGenerateVoucher(hbjsdId, VoucherType.FPHB);
            if (!begin)
                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            voucher = fphbService.getVoucher(jsdId,uuid,year,mouth,voucherMaster);
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            voucher.put("msgheader", msgheader);
            String r = voucherService.generateVoucher(voucher);
            result = xmlParse.getContent(r);
            //结果回写
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, hbjsdId, VoucherType.FPHB);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(new Gson().toJson(result));
    }
    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
            voucherService.checkJiuqiUsernamePassword();
            Boolean begin = voucherService.beginRevokeVoucher(hbjsdId, VoucherType.FPHB);
            if (!begin)
                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            Map<String, Object> revokeInfo = fphbService.getRevokeInfo(jsdId);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            //结果回写
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                Boolean success = voucherService.revokeVoucherSuccess(hbjsdId, VoucherType.FPHB);
                if (!success)
                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(new Gson().toJson(result));
    }
    //==================get and set ================================
    public String getHbrName() {
        return hbrName;
    }

    public void setHbrName(String hbrName) {
        this.hbrName = hbrName;
    }


    public Long getHbjsdId() {
        return hbjsdId;
    }

    public void setHbjsdId(Long hbjsdId) {
        this.hbjsdId = hbjsdId;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public String getGsJc() {
        return gsJc;
    }

    public void setGsJc(String gsJc) {
        this.gsJc = gsJc;
    }


    public Long getJsdId() {
        return jsdId;
    }

    public void setJsdId(Long jsdId) {
        this.jsdId = jsdId;
    }

    public Date getHbrqStart() {
        return hbrqStart;
    }

    public void setHbrqStart(Date hbrqStart) {
        this.hbrqStart = hbrqStart;
    }

    public Date getHbrqEnd() {
        return hbrqEnd;
    }

    public void setHbrqEnd(Date hbrqEnd) {
        this.hbrqEnd = hbrqEnd;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public VoucherService getVoucherService() {
        return voucherService;
    }

    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public YgService getYgService() {
        return ygService;
    }

    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }

    public List<Map> getVoucherItem() {
        return voucherItem;
    }

    public void setVoucherItem(List<Map> voucherItem) {
        this.voucherItem = voucherItem;
    }

    @Autowired
    public void setFphbService(FphbService fphbService) {
        this.fphbService = fphbService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }
}
